import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import {ReplaySubject} from 'rxjs';
import {CBoardEChartRender} from './utils/CBoardEChartRender';

declare var $: any;
import * as echarts from 'echarts';
import Echarts = echarts.Echarts;
import * as _ from 'underscore';
import * as numbro from 'numbro';
import {CBoardKpiRender} from './utils/CBoardKpiRender';
@Injectable()
export class ChartKpiService {

    constructor(
      private router: Router
    ) {}
    public render(containerDom, option, scope, persist){
      var render = new CBoardKpiRender(containerDom, option);
      var html = render.html(persist);
      if (scope) {
        // TODO
        // containerDom.append($compile(html)(scope));
      } else {
        containerDom.html(html);
      }
      return render.realTimeTicket();
    }

    public parseOption(data){
      var option = <any>{};
      var config = data.chartConfig;
      var casted_keys = data.keys;
      var casted_values = data.series;
      var aggregate_data = data.data;
      var newValuesConfig = data.seriesConfig;

      option.kpiValue = aggregate_data.length > 0 ? aggregate_data[0][0] : 'N/A';
      if (config.values[0].format) {
        option.kpiValue = numbro(option.kpiValue).format(config.values[0].format);
      }
      option.kpiName = config.values[0].name;
      option.style = config.values[0].style;
      option.edit = "编辑";
      option.refresh = "刷新";
      return option;
    }
}
